package com.itheima.web.controller.cargo;

import com.alibaba.dubbo.config.annotation.Reference;
import com.github.pagehelper.PageInfo;
import com.itheima.domain.cargo.*;
import com.itheima.service.cargo.FactoryService;
import com.itheima.service.cargo.ProductService;
import com.itheima.utils.FileUploadUtil;
import com.itheima.web.controller.BaseController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import java.util.Date;
import java.util.List;
import java.util.UUID;

@Controller
@RequestMapping("/cargo/factoryProduct")
public class FactoryProductController extends BaseController {

    @Reference
    private ProductService productService;

    @Reference
    private FactoryService factoryService;

    @Autowired
    private FileUploadUtil fileUploadUtil;

    @RequestMapping(value = "/list", name = "产品列表查询")
    public String list(
            @RequestParam(defaultValue = "1", name = "page") Integer pageNum,
            @RequestParam(defaultValue = "10") Integer pageSize) {
        ProductExample productExample = new ProductExample();
        productExample.setOrderByClause("create_time desc");
        PageInfo pageInfo = productService.findByPage(pageNum, pageSize,productExample);

        request.setAttribute("page", pageInfo);
        return "/cargo/factory/product-list";
    }


    @RequestMapping(value = "/toAdd", name = "跳转产品新增页面")
    public String toAdd() {
        //2. 查询所有生产货物的厂家列表
        FactoryExample factoryExample = new FactoryExample();
        factoryExample.createCriteria().andCtypeEqualTo("货物");
        List<Factory> factoryList = factoryService.findAll(factoryExample);
        request.setAttribute("factoryList", factoryList);
        return "/cargo/factory/product-add";
    }


    //新增和修改统一用一个方法处理
    @RequestMapping(value = "/edit", name = "产品新增")
    public String edit(Product product, MultipartFile productPhoto) {
        try {
            String filePath = fileUploadUtil.upload(productPhoto);
            product.setProductImage(filePath);
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("文件上传失败");
            throw new RuntimeException(e);
        }
        if (StringUtils.isEmpty(product.getId())) {
            //1. 设置主键
            product.setId(UUID.randomUUID().toString());

            product.setCreateTime(new Date());
            product.setInputTime(new Date());



            productService.save(product);
        } else {

            product.setInputTime(new Date());
            productService.update(product);
        }
        //重定向到list方法
        return "redirect:/cargo/factoryProduct/list.do?Id=" + product.getId();
    }



    @RequestMapping("/toUpdate")
    public String toUpdate(String id) {
        //根据id查询工厂信息
        Product product = productService.findById(id);
        request.setAttribute("product",product);
        //2. 查询所有生产货物的厂家列表
        FactoryExample factoryExample = new FactoryExample();
        factoryExample.createCriteria().andCtypeEqualTo("货物");
        List<Factory> factoryList = factoryService.findAll(factoryExample);
        request.setAttribute("factoryList", factoryList);

        return "cargo/factory/product-update";
    }




    @RequestMapping(value = "/delete", name = "产品删除")
    public String delete(String id) {
        //调用service删除
        productService.delete(id);

        //重定向到list方法
        return "redirect:/cargo/factoryProduct/list.do";
    }

    public String setFactoryIdByUUid() {
        return UUID.randomUUID().toString().substring(0, 4);
    }

}
